
package.path =  commonlib.FormatPath(__directory__ .. "../lua/?/init.lua;") .. package.path;
package.path =  commonlib.FormatPath(__directory__ .. "../lua/?.lua;") .. package.path;

local __require__ = _G.require;
local __require_modules__ = {};
local function G_require(module_name)
    if (__require_modules__[module_name] ~= nil) then return __require_modules__[module_name] end
    local module_path = string.gsub(module_name, "%.", "/");
    module_path = commonlib.FormatPath(__directory__ .. "../lua/" .. module_path .. ".lua");
    local module_text = commonlib.GetScriptTextByFilePath(module_path);
    if (module_text == "") then
        commonlib.__log_debug__("G_require require module:", module_name);
        commonlib.__xpcall__(function()
            __require_modules__[module_name] = __require__(module_name);
        end)
    else
        commonlib.__log_debug__("G_require import module:", module_path);
        __require_modules__[module_name] = import(module_path);
    end
    return __require_modules__[module_name];
end

_G.require = G_require;

commonlib.USE_LUV = LuaGui.USE_LUV;
commonlib.USE_LCURL = LuaGui.USE_LCURL;
commonlib.USE_LUA_CJSON = LuaGui.USE_LUA_CJSON;
commonlib.USE_LUA_EXPAT = LuaGui.USE_LUA_EXPAT;

local logger = import("./logger.lua");
local LuaJson = import("./LuaJson.lua")
local ljson = import("./ljson.lua");
local lxml = import("./lxml.lua");
local luv = import("./luv.lua");
local lcurl = import("./lcurl.lua");
local lbasexx = import("./lbasexx.lua");
local lmd5 = import("./lmd5.lua");
local lsha1 = import("./lsha1.lua");
local Jwt = import("./Jwt.lua");

-- json
if (not commonlib.USE_LUA_CJSON) then 
    commonlib.JsonEncode = function(o, pretty)
        if (pretty) then return LuaJson:encode_pretty(o) end 
        return LuaJson:encode(o);
    end

    commonlib.JsonDecode = function(s)
        return LuaJson:decode(s);
    end
end

-- xml
if (commonlib.USE_LUA_EXPAT) then
    commonlib.XmlEncode = function(o)
        return lxml.ToXmlString(o);
    end
    
    commonlib.XmlDecode = function(s)
        return lxml.FromXmlString(s);
    end
else  -- use tinyxml
    commonlib.XmlEncode = function(o)
        return LuaGui.Xml.ToXmlString(o);
    end
    
    commonlib.XmlDecode = function(s)
        return LuaGui.Xml.FromXmlString(s);
    end
end

-- jwt
commonlib.JwtEncode = function(data, key, expireFromNow, alg)
    return Jwt.encode(data, key, alg or "HS256", expireFromNow or 3600);
end

commonlib.JwtDecode = function(data, key, verify)
    return Jwt.decode(data, key, verify);
end

return {
    logger = logger,
    ljson = ljson,
    LuaJson = LuaJson,
    lxml = lxml;
    luv = luv,
    lcurl = lcurl,
    lbasexx = lbasexx,
    lmd5 = lmd5,
}